using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web.Hosting;

namespace Common.Core
{
	/// <summary>
	/// Provides methods for validating method parameters.
	/// </summary>
	public static class Guard
	{
		public static void IsOfType( Type type, Type value )
		{
			IsOfType( type, value, null );
		}

		public static void IsOfType( Type type, Type value, string message )
		{
			IsNotNull( type, "type" );
			IsNotNull( value, "value" );
			if ( !type.IsAssignableFrom( value ) )
			{
				throw new ArgumentException( message ?? string.Format( "Specified type '{0}' does not inherit from type '{1}'.", value, type ) );
			}
		}

		/// <summary>
		/// Determines whether value is less than the specified max.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="value">The value.</param>
		/// <param name="max">The max.</param>
		/// <param name="parameterName">Name of the parameter.</param>
		/// <exception cref="ArgumentOutOfRangeException">value is greater than or equal to max.</exception>
		public static void IsLessThan<T>(IComparable<T> value, T max, string parameterName)
		{
			if (value.CompareTo(max) >= 0)
				throw new ArgumentOutOfRangeException(parameterName);
		}

		/// <summary>
		/// Determines whether value is less than or equal to the specified max.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="value">The value.</param>
		/// <param name="max">The max.</param>
		/// <param name="parameterName">Name of the parameter.</param>
		/// <exception cref="ArgumentOutOfRangeException">value is greater than max.</exception>
		public static void IsLessThanOrEqual<T>(IComparable<T> value, T max, string parameterName)
		{
			if (value.CompareTo(max) > 0)
				throw new ArgumentOutOfRangeException(parameterName);
		}

		/// <summary>
		/// Determines whether value is null.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="value">The value.</param>
		/// <param name="parameterName">Name of the parameter.</param>
		/// <exception cref="ArgumentNullException">value is null.</exception>
		public static void IsNotNull<T>(T value, string parameterName)
		{
			if (value == null)
				throw new ArgumentNullException(parameterName);
		}

		/// <summary>
		/// Determines whether value is null or <see cref="string.Empty"/>.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <param name="parameterName">Name of the parameter.</param>
		/// <exception cref="ArgumentNullException">value is null or <see cref="string.Empty"/>.</exception>
		public static void IsNotNullOrEmpty(string value, string parameterName)
		{
			if (string.IsNullOrEmpty(value))
				throw new ArgumentNullException(parameterName);
		}
	}
}